using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using sbMembership.Library.Domain;
using sbMembership.Library.Presenters.ViewInterfaces;

namespace sbMembership.Library.Presenters
{
    public class EditUserEventArgs : EventArgs
    {
        private string userName;

        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
    }

    public class EditUserPresenter
    {
        private IEditUserView view;

        public EditUserPresenter(IEditUserView view)
        {
            if (view == null) throw new ArgumentNullException("view may not be null");

            this.view = view;
        }

        public EventHandler EditUserCompleteEvent;

        public void InitViewForEditing(string userName, bool isPostBack)
        {
            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException("userName may not be null or empty");

            if (!isPostBack)
            {
                MembershipUser user = Membership.GetUser(userName, false);
                view.UserToUpdate = new sbUser(user.UserName, "", user.Email);
            }
        }

        public void SaveUser()
        {
            string userName = view.UserToUpdate.UserName;
            MembershipUser user = Membership.GetUser(userName, false);
            user.Email = view.UserToUpdate.Email;
            Membership.UpdateUser(user);
            EditUserCompleteEvent(null, null);
        }
    }
}
